﻿Public Class clsTask
    Public Function SelectTask() As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM agv.task "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        If IsNothing(dt) Then
            SelectTask = New DataTable
        Else
            SelectTask = dt
        End If
    End Function

    Public Function SelectTaskCheck(lhj As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * from agv.task where lhj = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, lhj)
        If IsNothing(dt) Then
            SelectTaskCheck = New DataTable
        Else
            SelectTaskCheck = dt
        End If
    End Function

    Public Function SelectTaskStepByTaskId(task_id As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM agv.task_step where task_id = ? "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, task_id)
        If IsNothing(dt) Then
            SelectTaskStepByTaskId = New DataTable
        Else
            SelectTaskStepByTaskId = dt
        End If
    End Function

    Public Function save(en_task As entity_task, en_task_step1 As entity_task_step, en_task_step2 As entity_task_step) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        Try
            DBacs.BeginTransaction()
            szSql &= "insert into agv.task(type,prod_id,status,tm,update_time,lhj,prod_spec_id,mes_project_name) "
            szSql &= " values (?,?,?,?,(SELECT CURRENT_TIMESTAMP(6)),?,?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task.type, en_task.prod_id, en_task.status, en_task.tm, en_task.lhj, en_task.prod_spec_id, en_task.mes_project_name)
            szSql = String.Empty
            szSql &= "SELECT * from agv.task ORDER BY update_time desc limit 1"
            Dim dtT As DataTable = DBacs.ExecuteSql(szSql, -1)
            szSql = String.Empty
            szSql &= "insert into agv.task_step(prio,pick,drop,agv,task_id,status,prod_id,update_time,lhj,prod_spec_id) "
            szSql &= " values (?,?,?,?,?,?,?,(SELECT CURRENT_TIMESTAMP(6)),?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_step1.prio, en_task_step1.pick, en_task_step1.drop, en_task_step1.agv,
                                                        dtT.Rows(0)("sys_id"), en_task_step1.status, en_task_step1.prod_id, en_task_step1.lhj, en_task_step1.prod_spec_id)
            szSql = String.Empty
            szSql &= "SELECT * from agv.task_step ORDER BY update_time desc limit 1"
            Dim dtTS As DataTable = DBacs.ExecuteSql(szSql, -1)

            szSql = String.Empty
            szSql &= "insert into agv.task_step(prio,pick,drop,agv,task_id,status,prod_id,previous_id,update_time,lhj,prod_spec_id) "
            szSql &= " values (?,?,?,?,?,?,?,?,(SELECT CURRENT_TIMESTAMP(6)),?,?)"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， en_task_step2.prio, en_task_step2.pick, en_task_step2.drop, en_task_step2.agv,
                                                        dtT.Rows(0)("sys_id"), en_task_step2.status, en_task_step2.prod_id, dtTS.Rows(0)("sys_id"), en_task_step2.lhj, en_task_step2.prod_spec_id)

            If en_task.tm.Equals("1") Then
                szSql = String.Empty
                szSql &= "SELECT * from agv.task_step ORDER BY sys_id desc limit 1"
                Dim dtTS2 As DataTable = DBacs.ExecuteSql(szSql, -1)
                szSql = String.Empty
                szSql &= "insert into agv.tmj(task_step_id,agv,prod_id,update_time) "
                szSql &= " values (?,?,?,(SELECT CURRENT_TIMESTAMP(6)))"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, dtTS2.Rows(0)("sys_id"), en_task_step2.agv, en_task_step2.prod_id)
            End If
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        Finally
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

    Public Function TaskCancel(taskid As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        Try
            DBacs.BeginTransaction()
            szSql &= "SELECT * from agv.task_step where task_id = ?"
            Dim dtTa As DataTable = DBacs.ExecuteSql(szSql, -1, taskid)
            For index = 0 To dtTa.Rows.Count - 1
                If dtTa.Rows(index)("status").ToString().Contains("待创建") Then
                    szSql = String.Empty
                    szSql &= "update agv.task_step set status = ? where sys_id= ? "
                    rtn = DBacs.ExecuteUpdateSql(szSql, -1, "已取消", dtTa.Rows(index)("sys_id").ToString())
                End If
            Next
            szSql = String.Empty
            szSql &= "update agv.task set status = ? where sys_id= ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1， "已取消", taskid)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        Finally
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

End Class
